﻿/**
 * jQuery BaseUI 1.1
 * 
 * Copyright 2010 wangyajun [ wyajun@slof.com ]
 * 
 */
(function($) {
	function wrapFileText(target) {
		var options = $.data(target, "filetext").options;
		//清空内容
		$(target).empty();
		//设置样式
		$(target).addClass("filetext");
		//增加输入框
		var viewtext =  $("<input type=\"text\" class=\"filetext-viewText\" readonly />").appendTo(target);
		if(options.width){
			viewtext.css({width:options.width});
		}
		//显示框右侧的域
		var buttonFileDiv = $("<div class=\"filetext-buttonFile\" ></div>").appendTo(target);
		//file框
		var fhtml = "<input type=\"file\" ";
		if(options.name){
			fhtml = fhtml + "name=\""+options.name+"\" ";
		} 
		fhtml = fhtml + " />";
		var filefile = $(fhtml).appendTo(buttonFileDiv);
		if(options.value){
			filefile.attr("value",options.value);
			viewtext.attr("value",options.value);
		}
		
		var btn = $("<div></div>").appendTo(buttonFileDiv);
		//显示图片
		if(options.iconCls){
			btn.addClass(options.iconCls);
		} else {
			btn.addClass("icon-selectdoc");
		}
		//赋值给显示框
		filefile.unbind("change.filetext").bind("change.filetext",function(){
			filefile[0].select();
			setValue(target, document.selection.createRange().text);
		});
	};
	function checkValue(target, value){
		var options = $.data(target, "filetext").options;
		var viewtext = $(target).find("input[type=text]");
		if(options.permitType && options.permitType!="undefined"){
			//限定内容
			if(value){
				var pi = value.substring(value.lastIndexOf('.')+1);
				var pis = options.permitType.split(",");
				var flg = false;
				for(var i = 0;i < pis.length;i ++){
					if(pi.toUpperCase() == pis[i].toUpperCase()){
						flg = true;
						break;
					}
				}
				;
				if(flg){
					viewtext.removeClass("validatebox-invalid");
					viewtext.unbind(".validatebox")
				} else {
					viewtext.data("validatebox",{});
					//显示错误信息
					viewtext.addClass("validatebox-invalid");
					var interFunc = null;
			        viewtext.unbind(".validatebox").bind("focus.validatebox", function() {
						addTip(viewtext,$.fn.filetext.defaults.message.replace("0",options.permitType));
			        }).bind("blur.validatebox", function() {
			            removeTip(viewtext);
			        }).bind("mouseover.validatebox", function() {
						addTip(viewtext,$.fn.filetext.defaults.message.replace("0",options.permitType));
			        }).bind("mouseout.validatebox", function() {
			            removeTip(viewtext);
			        });	
				    /**
				     * 添加错误提示
				     * @param htmlDom
				     * @param errorMess
				     */
				    function addTip(htmlDom, errorMess) {
				        var tip = $.data(htmlDom[0], "validatebox").tip;
				        if (!tip) {
				            tip = $("<div class=\"validatebox-tip\">" + "<span class=\"validatebox-tip-content\">" + errorMess + "</span>" + "<span class=\"validatebox-tip-pointer\">" + "</span>" + "</div>").appendTo("body");
				            $.data(htmlDom[0], "validatebox").tip = tip;
				        }
				        tip.css({display:"block",left:htmlDom.offset().left + htmlDom.outerWidth(),top:htmlDom.offset().top});
				    }
				    //删除错误提示
				    function removeTip(htmlDom) {
				        var tip = $.data(htmlDom[0], "validatebox").tip;
				        if (tip) {
				            tip.remove();
				            $.data(htmlDom[0], "validatebox").tip = null;
				        }
				    }
				}
			}
		} 
	}
	function setValue(target, value) {
		checkValue(target, value);
		$(target).find("input[type=file]").val(value);
		$(target).find("input[type=text]").val(value);
	};
	$.fn.filetext = function(options,val) {
		if (typeof options == "string") {
			switch (options) {
				case "options" :
					return $.data(this[0], "options").options;
				case "setValue" :
					return this.each(function() {
								setValue(this, val);
							});
			}
		}
		options = options || {};
		return this.each(function() {
					var state = $.data(this, "filetext");
					if (state) {
						$.extend(state.options, options);
					} else {
						var t = $(this);
						$.data(this, "filetext", {
									options : $.extend({},
											$.fn.filetext.defaults, {
												name : t.attr("name"),
												width : t.attr("width"),
												permitType : t.attr("permitType"),
												iconCls : t.attr("iconCls")
											}, options)
								});
					}
					wrapFileText(this);
				});
	};
	$.fn.filetext.defaults = {
		name : null,
		width:null,
		permitType:null,
		message:"请选择正确的文件类型！文件类型允许为{0}",
		iconCls : null
	};
})(jQuery);
